#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
脑电波系统基础异常类
定义系统中使用的所有自定义异常
"""

class BrainWaveException(Exception):
    """脑电波系统基础异常类"""
    def __init__(self, message: str, error_code: str = None, details: dict = None):
        self.message = message
        self.error_code = error_code or "UNKNOWN_ERROR"
        self.details = details or {}
        super().__init__(self.message)
    
    def to_dict(self) -> dict:
        """转换为字典格式"""
        return {
            "error_code": self.error_code,
            "message": self.message,
            "details": self.details
        }

class DeviceException(BrainWaveException):
    """设备相关异常"""
    pass

class DeviceConnectionException(DeviceException):
    """设备连接异常"""
    def __init__(self, message: str, port: str = None):
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        detailed_message = f"[{timestamp}] 设备连接失败: {message}"
        if port:
            detailed_message += f" (端口: {port})"
        super().__init__(detailed_message, "DEVICE_CONNECTION_ERROR", {"port": port, "timestamp": timestamp})

class DeviceDataException(DeviceException):
    """设备数据异常"""
    def __init__(self, message: str, data_type: str = None):
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        detailed_message = f"[{timestamp}] 设备数据异常: {message}"
        if data_type:
            detailed_message += f" (数据类型: {data_type})"
        super().__init__(detailed_message, "DEVICE_DATA_ERROR", {"data_type": data_type, "timestamp": timestamp})

class SignalProcessingException(BrainWaveException):
    """信号处理异常"""
    def __init__(self, message: str, processing_step: str = None):
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        detailed_message = f"[{timestamp}] 信号处理异常: {message}"
        if processing_step:
            detailed_message += f" (处理步骤: {processing_step})"
        super().__init__(detailed_message, "SIGNAL_PROCESSING_ERROR", {"step": processing_step, "timestamp": timestamp})

class FilterException(SignalProcessingException):
    """滤波器异常"""
    def __init__(self, message: str, filter_type: str = None):
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        detailed_message = f"[{timestamp}] 滤波器异常: {message}"
        if filter_type:
            detailed_message += f" (滤波器类型: {filter_type})"
        super().__init__(detailed_message, "FILTER_ERROR", {"filter_type": filter_type, "timestamp": timestamp})

class BrainStateException(BrainWaveException):
    """脑状态分析异常"""
    def __init__(self, message: str, session_id: str = None):
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        detailed_message = f"[{timestamp}] 脑状态分析异常: {message}"
        if session_id:
            detailed_message += f" (会话ID: {session_id})"
        super().__init__(detailed_message, "BRAIN_STATE_ERROR", {"session_id": session_id, "timestamp": timestamp})

class ValidationException(BrainWaveException):
    """数据验证异常"""
    def __init__(self, message: str, field: str = None, value=None):
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        detailed_message = f"[{timestamp}] 数据验证异常: {message}"
        if field:
            detailed_message += f" (字段: {field}, 值: {value})"
        super().__init__(detailed_message, "VALIDATION_ERROR", {"field": field, "value": str(value), "timestamp": timestamp})

class SessionException(BrainWaveException):
    """会话异常"""
    def __init__(self, message: str, session_id: str = None):
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        detailed_message = f"[{timestamp}] 会话异常: {message}"
        if session_id:
            detailed_message += f" (会话ID: {session_id})"
        super().__init__(detailed_message, "SESSION_ERROR", {"session_id": session_id, "timestamp": timestamp})

class DatabaseException(BrainWaveException):
    """数据库异常"""
    def __init__(self, message: str, operation: str = None):
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        detailed_message = f"[{timestamp}] 数据库异常: {message}"
        if operation:
            detailed_message += f" (操作: {operation})"
        super().__init__(detailed_message, "DATABASE_ERROR", {"operation": operation, "timestamp": timestamp})

class ConfigurationException(BrainWaveException):
    """配置异常"""
    def __init__(self, message: str, config_key: str = None):
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        detailed_message = f"[{timestamp}] 配置异常: {message}"
        if config_key:
            detailed_message += f" (配置项: {config_key})"
        super().__init__(detailed_message, "CONFIGURATION_ERROR", {"config_key": config_key, "timestamp": timestamp})

class APIException(BrainWaveException):
    """API异常"""
    def __init__(self, message: str, status_code: int = 500, endpoint: str = None):
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        detailed_message = f"[{timestamp}] API异常: {message}"
        if endpoint:
            detailed_message += f" (端点: {endpoint}, 状态码: {status_code})"
        super().__init__(detailed_message, "API_ERROR", {"endpoint": endpoint, "status_code": status_code, "timestamp": timestamp})
        self.status_code = status_code